<?php
/**
 * Yii view _form
 *
 * @author nguyenmanhluu v1.0
 * @since Aug 8, 2013 - 7:43:59 PM
 * @author trongkhoi v1.1
 */
?>
<div class="box span12">
    <div class="box-header">
        <h2>
            <?php if ($model->isNewRecord): ?>
                <i class="icon-plus"></i><?= Yii::t('user', 'Add') ?>
            <?php else: ?>
                <i class="icon-edit"></i><?= Yii::t('user', 'Edit') ?>
            <?php endif; ?>
            <?php echo Yii::t('user', $this->title) ?>
        </h2>
        <div class="box-icon">
            <a href="<?= url(lcfirst($this->modelName).'/index') ?>" title="<?=Yii::t('user','Back')?>">
                <i class="icon-arrow-left"></i>
            </a>
        </div>
    </div>
    <div class="box-content">
        <?php
        $form_id = 'form';
        $form_id .= $model->isNewRecord ? '-add' : '-edit';
        $action = '';
        if (isset($model->createUrl) && isset($model->editUrl))
        {
            $action = ($model->isNewRecord) ? $model->createUrl : $model->editUrl;
        }

        $formOptions = array(
            'action' => $action,
            'id' => $form_id,
//            'enableClientValidation' => true,
            'enableAjaxValidation' => true,
            'htmlOptions' => array(
                'class' => 'form-horizontal',
                'enctype' => 'multipart/form-data',
            ),
            'clientOptions' => array(
                'validateOnSubmit' => true,
            ),
        );

        $form = $this->beginWidget('CActiveForm', $formOptions);
        ?>
        <?php echo CHtml::errorSummary($model); ?>
        <fieldset>
            <?php
            foreach ($this->formColumns as $column => $opts):
                //in case formColumn = array('attribute') ... no have value it's short write
                if (isset($this->modelName[$column]))
                    $column = $opts;
                ?>

                <div class="control-group">
                    <?php echo $form->labelEx($model, $column, array('class' => 'control-label')); ?>
                    <div class ="controls">
                        <?php
                        $type = isset($opts['type']) ? $opts['type'] : '';
                        switch ($type)
                        {
                            case 'textArea':
                                echo $form->textArea($model, $column, array(
                                    'style' => 'height:70px',
                                    'class' => 'span8',
                                    'placeholder' => $model->getAttributeLabel($column),
                                ));
                                break;

                            case 'editor':
                                echo $form->textArea($model, $column, array(
                                    'style' => 'height:70px;',
                                    'class' => 'cleditor',
                                    'placeholder' => $model->getAttributeLabel($column),
                                ));
                                break;

                            case 'wideEditor':
                                echo $form->textArea($model, $column, array(
                                    'style' => 'height:70px;',
                                    'class' => 'wideCleditor',
                                    'placeholder' => $model->getAttributeLabel($column),
                                ));
                                break;

                            case 'radioButtonList':
                                $data = isset($opts['data']) ? $opts['data'] : array();
                                echo $form->radioButtonList($model, $column, $data, array(
                                    'template' => '<label class="radio">{input} {label}</label>'
                                ));
                                break;

                            case 'dropDownList':
                                $data = isset($opts['data']) ? $opts['data'] : array();
                                echo $form->dropDownList($model, $column, $data);
                                break;
//
                            case 'dropDownListChosen':
                                $data = isset($opts['data']) ? $opts['data'] : array();
                                $select = isset($opts['selected']) ? $opts['selected'] : array();
                                echo CHtml::dropDownList($column . '[]', $select, $data, array(
                                    'multiple data-rel' => "chosen",
                                    'data-placeholder' => "Select some service..."
                                ));
                                break;
                            case 'inputPrepend':
                                echo '<div class="input-prepend">';
                                echo '<span class="add-on">' . $opts['pendValue'] . '</span>';
                                echo $form->textField($model, $column, array(
                                    'placeholder' => $model->getAttributeLabel($column),
                                    'class' => 'input-file uniform_on input-reset span6'
                                ));
                                echo '</div>';
                                break;
                            case 'inputAppend':
                                echo '<div class="input-append">';
                                echo $form->textField($model, $column, array(
                                    'placeholder' => $model->getAttributeLabel($column),
                                    'class' => 'input-file uniform_on input-reset span6'
                                ));
                                echo '<span class="add-on">' . $opts['pendValue'] . '</span>';
                                echo '</div>';
                                break;
                            case 'ckeditor':
                                $this->widget('ext.ckeditor.CKEditor', array(
                                    'model' => $model,
                                    'attribute' => $column,
                                    'editorTemplate' => 'full', // Toolbar settings (full, basic, advanced)
                                ));
                                break;
                            case 'ckfinder':
                                $field_name = CHTML::activeName($model, $column);
                                echo CHtml::image($opts['data'], $opts['alt'], array('id' => 'prev_' . $column, 'style' => 'max-height:160px;max-width:320px'));
                                echo CHtml::hiddenField($field_name, $opts['data'], array('id'=>"x$column"));
                                $this->widget('ext.ckeditor.EImageFinder', array('field' => $column, 'field_name'=>$field_name));
                                break;
                            case 'createUrl':
                                $des_id = CHTML::activeId($model, $opts['for']);
                                $des_id = "$('#$des_id')";
                                echo $form->textField($model, $column, array(
                                    'placeholder' => $model->getAttributeLabel($column),
                                    'class' => 'input-file uniform_on input-reset span6',
                                    'onkeyup' => "makeUrl($(this),$des_id)"
                                ));
                                break;
                            case 'datepicker':
                                echo $form->textField($model, $column, array(
                                    'placeholder' => $model->getAttributeLabel($column),
                                    'class' => 'input-file uniform_on input-reset span6 datepicker',
                                ));
                                break;
                            default:
                                echo $form->textField($model, $column, array(
                                    'placeholder' => $model->getAttributeLabel($column),
                                    'class' => 'input-file uniform_on input-reset span6'
                                ));
                                break;
                        }
                        ?>
                        <font color="red"><?php echo $form->error($model, $column); ?></font>
                    </div>
                </div>
            <?php endforeach; ?>
            <div class="clearfix"></div>
            
            <div class="form-actions">
                <?php $continue_label = $model->isNewRecord?Yii::t('user', 'Save and continue create'):Yii::t('user', 'Save and continue edit') ?>
                <button type="submit" class="btn btn-success" name="submit" value="submit"><i class="icon-ok"></i>
                    &nbsp;<?php echo Yii::t('user', 'Save') ?></button>&nbsp;
                <button class="btn btn-primary" type="submit" name="submit" value="continue"><i class="icon-ok"></i>
                    &nbsp; <?php echo $continue_label ?></button>&nbsp;
                <?php echo CHtml::resetButton(Yii::t('user','Reset'), array('class' => 'btn')); ?>
            </div>
        </fieldset>
        <?php $this->endWidget(); ?>
    </div>
</div>
<script>
    function makeUrl(original, destination) {
        title = original.val();
        destination.val(convertToSlug(title));
    }
    function convertToSlug(Text)
    {
        // Xử lý ký tự Unicode
        in_chrs = 'áàảãạăắặằẳẵâấầẩẫậđéèẻẽẹêếềểễệóòỏõọôốồổỗộơớờởỡợúùủũụưứừửữựýỳỷỹỵÁÀẢÃẠĂẮẶẰẲẴÂẤẦẨẪẬĐÉÈẺẼẸÊẾỀỂỄỆÍÌỈĨỊíìỉĩịÓÒỎÕỌÔỐỒỔỖỘƠỚỜỞỠỢÚÙỦŨỤƯỨỪỬỮỰÝỲỶỸỴ',
                out_chrs = 'aaaaaaaaaaaaaaaaadeeeeeeeeeeeooooooooooooooooouuuuuuuuuuuyyyyyAAAAAAAAAAAAAAAAADEEEEEEEEEEEIIIIIiiiiiOOOOOOOOOOOOOOOOOOUUUUUUUUUUUUYYYYY'
        transl = {};
        eval('var chars_rgx = /[' + in_chrs + ']/g');
        for (var i = 0; i < in_chrs.length; i++)
        {
            transl[in_chrs.charAt(i)] = out_chrs.charAt(i);
        }
        Text = Text.replace(chars_rgx, function(match) {
            return transl[match];
        });
        Text = Text.toLowerCase().replace(/ /g, '-').replace(/[^\w-]+/g, '').replace(/--/g, '-');
        // Bỏ đi các ký tự đặc biệt
        return Text;
    }
    $('.wideCleditor').cleditor({
        width: 800 // width not including margins, borders or padding
    });
</script>